﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Clients_Control_hotro : System.Web.UI.UserControl
{
    private String strDetail = "<p><b>$title</b></p><br/>$body";
    protected void Page_Load(object sender, EventArgs e)
    {
        
            int id = -1;
            try
            {
                id = int.Parse(Request.Params["id"].ToString());
            }
            catch
            {
            }
            if (id == -1)
            {
                LoadHoTro();
            }
            else {
                String strTitle = "";//title
                try {
                    strTitle = Request.Params["title"].ToString().Trim();
                }
                catch { }
                Boolean flag = false;
                if (!string.IsNullOrEmpty(strTitle)) {
                    int index = strTitle.LastIndexOf("_");
                    if(index!=-1)
                        strTitle = strTitle.Substring(index);
                    if (strTitle == "2" || strTitle == "_2")
                        flag = true;
                }
                if (!flag)
                    LoadDetail(id);
                else
                    DownLoad(id);
            }
            if (!this.IsPostBack)
            {
                String strPage = "";
                try
                {
                    strPage = Request.Params["Pro"].ToString().Trim();
                }
                catch
                {

                }
                lblContentTitle.Text = Constant.ContentTitle(strPage);
            }
    }



    private void LoadHoTro() {
        WebData db = new WebData();
        this.grdDs.DataSource = db.tbl_Hotros.GetListCache("hotrokt", Constant.time);
        this.grdDs.DataBind();
        detail.Visible = false;
        hotro.Visible = true;
    }

    private void LoadDetail(int id) {
        WebData data = new WebData();
        DataLibrary.tbl_News_Lang item = data.tbl_News_Langs.GetListCache("hotroDetail", Constant.time).SingleOrDefault(p => p.IdNew == id && p.IdLang == 1);
        String url = "";

        if (item != null)
        {
            url = url + strDetail.Replace("$title", item.TitleNew).Replace("$body", item.BodyNew);
        }
        lblDetail.Text = url;
        detail.Visible = true;
        hotro.Visible = false;
    }

    private void DownLoad(int id) {
        WebData data = new WebData();
        DataLibrary.tbl_Download item = data.tbl_Downloads.GetListCache("hotroDetailDownload", Constant.time).SingleOrDefault(p => p.Id == id);
        String url = "";
        String img = "";
        if (item != null)
        {
            url = url + strDetail.Replace("$title", item.Mota).Replace("$body", item.Description);
            img = "<p><a href='" + Constant.ROOT_PATH + item.Path + "'>"
            + "<img src='" + Constant.ROOT_PATH  + "Clients/images/taive.gif'></a></p>";
            url =url+ img;
        }

        lblDetail.Text = url;
        detail.Visible = true;
        hotro.Visible = false;
    }
}